package commons.base.serialization.json;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.xml.bind.annotation.XmlElement;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

public class JsonExample {

	public static void main(String[] args)throws Exception {
		JSONObject json = JSON.parseObject(readJson("user_buyer_get_response.json"));
		if(json.containsKey("user_buyer_get_response")){
			UserBuyerGetResponse response = JSON.toJavaObject(json.getJSONObject("user_buyer_get_response"), UserBuyerGetResponse.class);
//			System.err.println(response.getUser().getNick());
		}
		
		JSONObject errorJson = JSON.parseObject(readJson("error_response.json"));
		if(errorJson.containsKey("error_response")){
			ErrorResponse errorResponse = JSON.toJavaObject(errorJson.getJSONObject("error_response"), ErrorResponse.class);
			System.err.println(errorResponse.getCode());
			System.err.println(errorResponse.getMsg());
			System.err.println(errorResponse.getSubCode());
			System.err.println(errorResponse.getSubMsg());
			System.err.println(JSON.toJSONString(errorResponse));
		}

//		UserBuyerGetResponse response = parseObject("", "user_buyer_get_response", UserBuyerGetResponse.class);
	}
	
	// or parseArray List<T>
	public static <T> T parseObject(String text, String fieldName, Class<T> clazz){
		JSONObject json = JSON.parseObject(text);
		
		if(json.containsKey("error_response")){
			ErrorResponse errorResponse = JSON.toJavaObject(json.getJSONObject("error_response"), ErrorResponse.class);
			// ErrorResponse extends RuntimeException ?
			throw new RuntimeException("error: " + errorResponse.toString());
		}
		
		if(json.containsKey(fieldName)){
			return JSON.toJavaObject(json.getJSONObject(fieldName), clazz);
		}
		
		throw new RuntimeException("illegal response: expect "+fieldName+" but " + text);
	}
	private static String readJson(String name) throws Exception{
		Path path = Paths.get(JsonExample.class.getResource("/" + name).toURI());
		
		return new String(Files.readAllBytes(path), "UTF-8");
	}
	
	public static class UserBuyerGetResponse {

	    private User       user;

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

	}
	
	public static class User {
		@XmlElement(name="nick") 
	    private String nick;
		@XmlElement(name="sex") 
	    private String sex;
		@XmlElement(name="avatar") 
	    private String avatar;
	    
//		public String getNick() {
//			return nick;
//		}
		public void setNick(String nick) {
			this.nick = nick;
		}
//		public String getSex() {
//			return sex;
//		}
		public void setSex(String sex) {
			this.sex = sex;
		}
//		public String getAvatar() {
//			return avatar;
//		}
		public void setAvatar(String avatar) {
			this.avatar = avatar;
		}

	}
	
	public static class ErrorResponse {
		private int code;
		private String msg;
		@JSONField(name="sub_code")
		private String subCode;
		@JSONField(name="sub_msg")
		private String subMsg;
		// 忽略字段
		@JSONField(serialize=false, deserialize=false)
		private String igoneField;
		
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public String getSubCode() {
			return subCode;
		}
		public void setSubCode(String subCode) {
			this.subCode = subCode;
		}
		public String getSubMsg() {
			return subMsg;
		}
		public void setSubMsg(String subMsg) {
			this.subMsg = subMsg;
		}
		
		@Override
		public String toString(){
			return code + "," + msg + "," + subCode + "," + subMsg;
		}
		public String getIgoneField() {
			return igoneField;
		}
		public void setIgoneField(String igoneField) {
			this.igoneField = igoneField;
		}
		
	}

}
